package mine.code.question.other;

import org.junit.Test;

public class 外观数列 {
    @Test
    public void result() {
        System.out.println(countAndSay(6));
    }

    public String countAndSay(int n) {
        if (n == 1) return "1";
        String str = countAndSay(n - 1);
        char[] arr = str.toCharArray();
        StringBuilder re = new StringBuilder();
        int sum = 1;
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                re.append(sum).append(arr[i]);
                break;
            }
            if (arr[i] != arr[i + 1]) {
                re.append(sum).append(arr[i]);
                sum = 1;
            } else {
                sum++;
                if (i == arr.length - 1) {
                    re.append(sum).append(arr[i]);
                    break;
                }
            }
        }
        return re.toString();
    }
}
